﻿#pragma once
#include "OCMain/FileUtil.h"
#include "OCMain/Log.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"

using namespace OCPP;

#if 1
void  ASCToBin()
{
	//String output = "\\\\10.104.207.155\\Cesium\\Assets\\2024\\Shiyan\\LvJiaHe";
	//String dataPath = U2A("I:\\Project\\2024\\长科院\\长安大学\\吕家河滑坡");

	String output = "\\\\10.104.207.155\\Cesium\\Assets\\2024\\Shiyan\\WangJiaYuan";
	String dataPath = U2A("I:\\Project\\2024\\长科院\\长安大学\\王家院滑坡");

	FileUtil::makeDirectory(output);

	auto fileVec = FileUtil::findFiles(dataPath);
	for (auto& file : fileVec)
	{
		if (!StringUtil::endsWith(file, ".asc"))
		{
			continue;
		}

		String fileName = joinPath(dataPath, file);
		std::ifstream ifs(fileName);
		if (ifs)
		{
			String line;

			rapidjson::StringBuffer buffer;
			rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
			writer.StartObject();

			writer.SetMaxDecimalPlaces(8);
			for (int i = 0; i < 6; i++)
			{
				getline(ifs, line);
				StringVector strVec = StringUtil::split(line, " ");
				writer.Key(strVec[0].c_str());
				if (i < 2)
				{
					writer.Int(std::stoi(strVec[1]));
				}
				else
				{
					writer.Double(std::stof(strVec[1]));
				}
			}


			writer.Key("data");
			writer.StartArray();
			while (getline(ifs, line))
			{
				StringVector strVec = StringUtil::split(line, " ");
				for (auto floatStr : strVec)
				{
					if (!floatStr.empty())
					{
						float v = std::stof(floatStr);
						writer.Double(v);
					}
				}
			}
			ifs.close();
			writer.EndArray();

			writer.EndObject();

			String outFileName = joinPath(output, file.substr(0, file.length() - 3) + "json");
			std::ofstream ofs(outFileName);
			ofs << buffer.GetString();
			ofs.close();
		}
	}
}
#endif

#if 0
void  ASCToBin()
{
	String output = "\\\\10.104.207.155\\Cesium\\Assets\\2024\\Shiyan";
	String fileName = U2A("I:\\Project\\2024\\长科院\\长安大学\\王家院滑坡\\h_0.dat");
	OCPP_LOG_INFO << fileName << "\n";

	std::ifstream ifs(fileName);
	if (ifs)
	{
		String line;

		rapidjson::StringBuffer buffer;
		rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
		writer.StartArray();
		writer.SetMaxDecimalPlaces(8);

		while (getline(ifs, line))
		{
			StringVector strVec = StringUtil::split(line, "  ");
			float x = std::stof(strVec[0].c_str());
			float y = std::stof(strVec[2].c_str());
			float z = std::stof(strVec[4].c_str());
			writer.Double(x);
			writer.Double(y);
			writer.Double(z);
		}
		ifs.close();
		writer.EndArray();
		std::ofstream ofs(joinPath(output, "h_0.json"));
		ofs << buffer.GetString();
		ofs.close();
		}
	}
#endif
